package com.yanqu.road.dao.impl.activity.liondance;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.liondance.config.LionDanceCardInfo;
import com.yanqu.road.entity.activity.liondance.config.LionDanceCombInfo;
import com.yanqu.road.entity.activity.liondance.config.LionDanceJumpConfigInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class LionDanceConfigDaoImpl extends TempDao {

    private static String getLionDancePassConfigInfoSql = "select * from `t_s_activity_liondance_jump` where `activityId`=? ORDER BY `id` ASC";
    public List<LionDanceJumpConfigInfo> getLionDancePassConfigInfoList(int activityId) {
        List<LionDanceJumpConfigInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLionDancePassConfigInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LionDanceJumpConfigInfo data = new LionDanceJumpConfigInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setPileNum(rs.getInt("pileNum"));
                data.setColorNumList(StringUtils.stringToIntegerList(rs.getString("colorPilePosition"), ";"));
                data.setFackWeightList(StringUtils.stringToIntegerList(rs.getString("pileFace"), ";"));
                data.setColumnWeightList(StringUtils.stringToIntegerList(rs.getString("pileColumn"), ";"));
                data.setDynamicWeightList(StringUtils.stringToIntegerList(rs.getString("dynamic"), ";"));
                data.setGapRangeList(StringUtils.stringToIntegerList(rs.getString("flowerPileDistance"), ";"));
                data.setAddItemPosition(StringUtils.stringToIntegerList(rs.getString("addItemPosition"), ";"));
                data.setAddItem(rs.getString("addItem"));
                data.setCollectItemPosition(StringUtils.stringToIntegerList(rs.getString("collectItemPosition"), ";"));
                data.setCollectItem(rs.getString("collectItem"));
                data.setCollectItemType(StringUtils.stringToIntegerList(rs.getString("collectItemType"), ";"));
                data.setPileHighWeighList(StringUtils.stringToIntegerList(rs.getString("pileHigh"), ";"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getLionDanceCardInfoSql = "select * from `t_s_activity_liondance_card` where `activityId`=? ORDER BY `id` ASC";
    public List<LionDanceCardInfo> getLionDanceCardInfoList(int activityId) {
        List<LionDanceCardInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLionDanceCardInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LionDanceCardInfo data = new LionDanceCardInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setSort(rs.getInt("sort"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getLionDanceCombInfoSql = "select * from `t_s_activity_liondance_comb` where `activityId`=? ORDER BY `combId` ASC";
    public List<LionDanceCombInfo> getLionDanceCombInfoList(int activityId) {
        List<LionDanceCombInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getLionDanceCombInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LionDanceCombInfo lionDanceCombInfo = new LionDanceCombInfo();
                lionDanceCombInfo.setActivityId(rs.getInt("activityId"));
                lionDanceCombInfo.setCombId(rs.getInt("combId"));
                lionDanceCombInfo.setName(rs.getString("name"));
                lionDanceCombInfo.setElement(PropertyHelper.parseStringToProperty(rs.getString("element"), "\\|"));
                lionDanceCombInfo.setRewards(rs.getString("rewards"));
                list.add(lionDanceCombInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
